package leetcode.BinaryTree;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class PostorderTraversal {

    /**
     * 二叉树后序遍历
     *
     * @param root
     * @return
     */
    public static List<Integer> postorderTraversal(TreeNode root) {
        List<Integer> list = new ArrayList<>();
        getIntegers(root, list);
        return list;
    }

    private static void getIntegers(TreeNode root, List<Integer> list) {
        if (root != null) {
            getIntegers(root.left, list);
            getIntegers(root.right, list);
            list.add(root.val);
        }
    }

    public static void main(String[] args) {
        TreeNode node1 = new TreeNode(1);
        TreeNode node2 = new TreeNode(2);
        TreeNode node3 = new TreeNode(3);

        node1.right = node2;
        node2.left = node3;

        System.out.println(Arrays.asList(postorderTraversal(node1)));

    }
}
